<?php
require_once( 'mofachi.DataConnection.php' );
class UserBabyData {
	private $field = array();
	private $relations = array();
	public function __get( $key ) {
		if( !isset( $this->field[ $key ] ) ) {
            return null;
        }
		return $this->field[ $key ];
	}
	public function __set( $key, $value ) {
		$this->field[ $key ] = $value;
	}
	public function __construct() {
		$this->field[ "user_id" ] = null;
		$this->field[ "baby_id" ] = null;
		$this->field[ "relation" ] = null;
	}
	public function add() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "insert into user_baby values(".$this->field[ "user_id" ].",";
		$query.= $this->field[ "baby_id" ].",".$this->field[ "relation" ].")";
		if( !$db->query($query) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	private function loadData( $result ) {
		$num = $result->num_rows;
		for( $i = 0; $i < $num; $i++ ) {
			$row = $result->fetch_assoc();
			$link = array();
			$link[ "user_id" ] = $row[ "user_id" ];
			$link[ "baby_id" ] = $row[ "baby_id" ];
			$link[ "relation" ]= $row[ "relation" ];
			$this->relations[ $i ] = $link;
		}
	}
	public function loadByID() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from user_baby where user_id=".$this->field[ "user_id" ];
		$query.= " and baby_id=".$this->field[ "baby_id" ];
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;		
		}
		$row = $result->fetch_assoc();
		$this->field[ 'relation' ] = $row[ 'relation' ];
		$this->loadData( $result );

		$result->free();
		$db->close();
		return $this->relations;
	}
	public function loadByUserID() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from user_baby where user_id=".$this->field[ "user_id" ];
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;		
		}
		$this->loadData( $result );

		$result->free();
		$db->close();
		return $this->relations;
	}
	public function loadByBabyID() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from user_baby where baby_id=".$this->field[ "baby_id" ];
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;		
		}
		$this->loadData( $result );

		$result->free();
		$db->close();
		return $this->relations;
	}
	public function update() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "update user_baby set relation=".$this->field[ "relation" ];
		$query.= " where user_id=".$this->field[ "user_id" ]." and baby_id=".$this->field[ "baby_id" ];
		if( !$db->query($query) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	public function remove() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "delete from user_baby where user_id=".$this->field[ "user_id" ];
		$query.= " and baby_id=".$this->field[ "baby_id" ];
		$db->query( $query );
		$db->close();
		return true;
	}
	//-------------------------------------------------------------------------------------
	// static functions
	//-------------------------------------------------------------------------------------	
	public static function listData( $offset, $length ) {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = 'select ub.user_id,u.nickname as user,ub.baby_id,b.nickname as baby,ub.relation ';
		$query.= 'from user_baby ub left join userinfo u on ub.user_id = u.user_id ';
		$query.= 'left join babyinfo b on ub.baby_id = b.baby_id ';
		$query.= 'order by user_id desc, baby_id desc limit '.$offset.', '.$length;
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$returnArray = array();
		$num = $result->num_rows;
		for( $i = 0; $i < $num; $i++ ) {
			$row = $result->fetch_assoc();
			$link = array();
			$link[ "user_id" ] = $row[ "user_id" ];
			$link[ "user" ] = $row[ "user" ];
			$link[ "baby_id" ] = $row[ "baby_id" ];
			$link[ "baby" ] = $row[ "baby" ];
			$link[ "relation" ]= $row[ "relation" ];
			$returnArray[ $i ] = $link;
		}
		$result->free();
		$db->close();
		return $returnArray;
	}
}
?>